﻿using System;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Tasks;


namespace LuyenThiVer03
{
    public partial class PageChonMon
    {
        private string _kieuthi = "";

        public PageChonMon()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                _kieuthi = NavigationContext.QueryString["chon"];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            base.OnNavigatedTo(e);
        }

        private void btToan_OnClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Co so du lieu dang cap nhat.");
            
            //if (kieuthi == "thithu")
            //{
            //    NavigationService.Navigate(new Uri("/PageChonDe.xaml?mon=57", UriKind.Relative));
            //}
            //else if (kieuthi == "onthi")
            //{
            //    NavigationService.Navigate(new Uri("/PageSoCauOnThi.xaml?mon=57", UriKind.Relative));
            //}
            //else
            //{
            //    NavigationService.Navigate(new Uri("/PageTraCuu.xaml?mon=toan", UriKind.Relative));
            //}
        }

        private void btLy_OnClick(object sender, RoutedEventArgs e)
        {
            HandleClickForButtons("76", "ly");
            ((App)Application.Current).currentMon = "Lý";
        }

        private void btHoa_OnClick(object sender, RoutedEventArgs e)
        {
            HandleClickForButtons("75", "hoa");
            ((App)Application.Current).currentMon = "Hóa";
        }

        private void btAnh_OnClick(object sender, RoutedEventArgs e)
        {
            HandleClickForButtons("61", "anh");
            ((App)Application.Current).currentMon = "Anh";
        }

        private void btSinh_OnClick(object sender, RoutedEventArgs e)
        {
            HandleClickForButtons("74", "sinh");
            ((App)Application.Current).currentMon = "Sinh";
        }

        private void btSu_OnClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Co so du lieu dang cap nhat.");
            //if (kieuthi == "thithu")
            //{
            //    NavigationService.Navigate(new Uri("/PageChonDe.xaml?mon=su", UriKind.Relative));
            //}
            //else if (kieuthi == "onthi")
            //{
            //    NavigationService.Navigate(new Uri("/PageSoCauOnThi.xaml?mon=su", UriKind.Relative));
            //}
            //else
            //{
            //    NavigationService.Navigate(new Uri("/PageTraCuu.xaml?mon=su", UriKind.Relative));
            //}
        }

        private void btDia_OnClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Co so du lieu dang cap nhat.");
            //if (kieuthi == "thithu")
            //{
            //    NavigationService.Navigate(new Uri("/PageChonDe.xaml?mon=dia", UriKind.Relative));
            //}
            //else if (kieuthi == "onthi")
            //{
            //    NavigationService.Navigate(new Uri("/PageSoCauOnThi.xaml?mon=dia", UriKind.Relative));
            //}
            //else
            //{
            //    NavigationService.Navigate(new Uri("/PageTraCuu.xaml?mon=dia", UriKind.Relative));
            //}
        }

        private void btVan_OnClick(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Co so du lieu dang cap nhat.");
            //if (kieuthi == "thithu")
            //{
            //    NavigationService.Navigate(new Uri("/PageChonDe.xaml?mon=van", UriKind.Relative));
            //}
            //else if (kieuthi == "onthi")
            //{
            //    NavigationService.Navigate(new Uri("/PageSoCauOnThi.xaml?mon=van", UriKind.Relative));
            //}
            //else
            //{
            //    NavigationService.Navigate(new Uri("/PageTraCuu.xaml?mon=van", UriKind.Relative));
            //}
        }

        private void HandleClickForButtons(string maSoMon, string tenMon)
        {
            if (IsNetworkDataNotAvailable) return;
            switch (_kieuthi)
            {
                case "thithu":
                    NavigationService.Navigate(new Uri("/PageChonDe.xaml?mon="+maSoMon, UriKind.Relative));
                    break;
                case "onthi":
                    NavigationService.Navigate(new Uri("/PageChuyenDe.xaml?mon="+maSoMon, UriKind.Relative));
                    break;
                default:
                    NavigationService.Navigate(new Uri("/PageTraCuu.xaml?mon="+tenMon, UriKind.Relative));
                    break;
            }
        }

        private bool IsNetworkDataNotAvailable
        {
            get
            {
                if (!DeviceNetworkInformation.IsNetworkAvailable)
                {
                    var messageBox = new CustomMessageBox
                    {
                        Caption = "Không thể kết nối internet!",
                        Message = "Hãy kết nối 3g hay wifi để tiếp tục!",
                        LeftButtonContent = "Kêt nối wifi",
                        RightButtonContent = "Kết nối 3g"
                    };
                    messageBox.Dismissed += (s1, e1) =>
                    {
                        switch (e1.Result)
                        {
                            case CustomMessageBoxResult.LeftButton:
                                var connectionSettingsTaskWifi = new ConnectionSettingsTask { ConnectionSettingsType = ConnectionSettingsType.WiFi };
                                connectionSettingsTaskWifi.Show();
                                break;
                            case CustomMessageBoxResult.RightButton:
                                var connectionSettingsTask3G = new ConnectionSettingsTask { ConnectionSettingsType = ConnectionSettingsType.Cellular };
                                connectionSettingsTask3G.Show();
                                break;
                            case CustomMessageBoxResult.None:
                                // Do something.
                                break;
                        }
                    };
                    messageBox.Show();
                    return true;
                }
                return false;
            }
        }
    }
}